package com.lanou.pianke.articlecomment.web.controller;

import com.lanou.pianke.articlecomment.entity.ArticleComment;
import com.lanou.pianke.articlecomment.entity.ArticleReply;
import com.lanou.pianke.articlecomment.service.ArticleCommentService;
import com.lanou.pianke.articlecomment.service.ArticleReplyService;
import com.lanou.pianke.common.utils.RandomString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleCommentController {

    @Autowired
    private ArticleCommentService articleCommentService;

    @Autowired
    private ArticleReplyService articleReplyService;

    @GetMapping("/comments")
    public List<ArticleComment> showArticleComments(String contentId){
        return articleCommentService.selectCommentInfo(contentId);
    }


    @PostMapping("/publishComment")
    public void publishComment(String contentId,String content,Long uid,Long autherId){

        String commentId = RandomString.getRandomString();
        long addTime = new Date().getTime();
        ArticleComment articleComment = new ArticleComment(commentId,contentId,uid,autherId,addTime,content);
        articleCommentService.insertArticleComment(articleComment);
    }

    //查看所有举报文章评论 by李洋
    @GetMapping("/showArticleComments")
    public List<ArticleComment> showArticleComments(){
        //查看所有举报文章评论
        List<ArticleComment> commentList = articleCommentService.showAll();
        for (ArticleComment articleComment : commentList) {
            String id = articleComment.getAcomid();
            //通过id查看该文章评论下的所有文章回复
            List<ArticleReply> areplyList = articleReplyService.selectById(id);
            if (areplyList != null){
                articleComment.setAreplyList(areplyList);
            }
        }
        return commentList;
    }

}
